package com.smartappsnetwork.personalexpenses.adapters;

import java.util.Vector;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.smartappsnetwork.personalexpenses.R;
import com.smartappsnetwork.personalexpenses.colorpicker.ColorPickerDialog;
import com.smartappsnetwork.personalexpenses.colorpicker.ColorPickerDialog.OnColorSelectedListener;
import com.smartappsnetwork.personalexpenses.dataaccess.CategoryDA;
import com.smartappsnetwork.personalexpenses.dataobject.CategoryDO;

@SuppressLint("DefaultLocale")
public class CategoryAdapter extends BaseAdapter
{
	private Context context;
	private Vector<CategoryDO> vecCategoryDO;
	private boolean hideIcon = true;
	private boolean viewClicable = true;
	
	public CategoryAdapter(Context context, Vector<CategoryDO> vecCategoryDO)
	{
		this.context = context;
		this.vecCategoryDO = vecCategoryDO;
	}
	
	public void setHideIcon(boolean hideIcon)
	{
		this.hideIcon = hideIcon;
	}
	
	public void setViewClicable(boolean viewClicable)
	{
		this.viewClicable = viewClicable;
	}
	
	public void refreshList(Vector<CategoryDO> vecCategoryDO)
	{
		this.vecCategoryDO = vecCategoryDO;
		notifyDataSetChanged();
	}
	
	@Override
	public int getCount() {
		return vecCategoryDO.size();
	}

	@Override
	public CategoryDO getItem(int position) {
		return vecCategoryDO.get(position);
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@SuppressLint("DefaultLocale")
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) 
	{
		convertView = LayoutInflater.from(context).inflate(R.layout.category_item, null);
		
		TextView tvItemColor = (TextView) convertView.findViewById(R.id.tvItemColor);
		TextView tvItemName = (TextView) convertView.findViewById(R.id.tvItemName);
		tvItemName.setText(vecCategoryDO.get(position).name);
		tvItemColor.setBackgroundColor((int)vecCategoryDO.get(position).colorcode);
		String c = vecCategoryDO.get(position).name.toUpperCase();
		tvItemColor.setText(c.charAt(0)+"");
		
		if(hideIcon)
			tvItemColor.setVisibility(View.VISIBLE);
		else
			tvItemColor.setVisibility(View.GONE);

		if(viewClicable)
		{
			convertView.setOnClickListener(new OnClickListener() 
			{
				@Override
				public void onClick(View v) 
				{
				        
				        final AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
						LinearLayout view = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.add_category_dialog, null); 
						alertDialog.setView(view);
						final AlertDialog dialog = alertDialog.create();
						final TextView tvtitle = (TextView) view.findViewById(R.id.tvTitle);
						tvtitle.setText("Modify Category");
						final EditText edtCategoryName = (EditText) view.findViewById(R.id.edtCategoryName);
						final TextView tvColorCode = (TextView) view.findViewById(R.id.tvColorCode);
						tvColorCode.setBackgroundColor((int)vecCategoryDO.get(position).colorcode);
						
						final CategoryDO categoryDO = new CategoryDO();
						
						edtCategoryName.setText(vecCategoryDO.get(position).name);
						categoryDO.colorcode = vecCategoryDO.get(position).colorcode;
						
						tvColorCode.setOnClickListener(new OnClickListener() 
						{
							@Override
							public void onClick(View v) 
							{
						        ColorPickerDialog colorPickerDialog = new ColorPickerDialog(context, 123412, new OnColorSelectedListener() {
									
									@Override
									public void onColorSelected(int color) {
										tvColorCode.setBackgroundColor(color);
										categoryDO.colorcode = color;
									}
								});
						        colorPickerDialog.show();
							}
						});
						
						edtCategoryName.setText(vecCategoryDO.get(position).name);
						
						Button btnOk = (Button) view.findViewById(R.id.btnOk);
						Button btnCancel = (Button) view.findViewById(R.id.btnCancel);
						btnOk.setOnClickListener(new OnClickListener() 
						{
							@Override
							public void onClick(View v) 
							{
								if(edtCategoryName.getText().toString().equalsIgnoreCase(""))
								{
									Toast.makeText(context, "Please enter the complete information!", Toast.LENGTH_SHORT).show();
								}
								else
								{
									final String currency = edtCategoryName.getText().toString();
									categoryDO.name = currency;
									categoryDO.cid = vecCategoryDO.get(position).cid;
									new CategoryDA().updateCategory(categoryDO);
									dialog.dismiss();
									refreshList(new CategoryDA().getAllCategory());
								}
							}
						});
						btnCancel.setOnClickListener(new OnClickListener() 
						{
							
							@Override
							public void onClick(View v) 
							{
								dialog.dismiss();
							}
						});
						dialog.show();
						
					}
				});
		}
		return convertView;
	}
	
}
